<?php
namespace easemob;

class Group extends IM
{
       
    function getAllGroups($limit=0,$cursor=''){
        $url=$this->geturl('chatgroups');
       $query='';
        if($limit){
            $query="limit=$limit";
        }
        if($cursor){
            if($query){
                $query.="&cursor=$cursor";
            }else{
                $query="cursor=$cursor";
            }
        } 
        
        if($query){
            $url="$url?$query";
        }
        $ret=$this->sentGet($url);
        
        return isset($ret['data'])?$ret['data']:false;
    }
    
    function create($owner,$groupname,$desc){
        $url=$this->geturl('chatgroups');
        $data=[        
            "groupname"=> $groupname,
            "desc"=>$desc,
            "public"=> true,
            //"maxusers"=> 0,
            "approval"=> true,
            "owner"=>$owner,
            //"members"=>[]
        ];
        
        $ret=$this->sentPost($url, $data);
        return isset($ret['data']['groupid'])?$ret['data']['groupid']:false;
        //return $ret; 67096783421441
    }
    
    function getGroupUsers($groupid){         
        $url=$this->geturl('chatgroups/'.$groupid.'/users');
        $ret=$this->sentGet($url);
        return isset($ret['data'])?$ret['data']:false;
      
    }
    
    function changeOwner($groupid,$newowner){
      
        $url=$this->geturl('chatgroups/'.$groupid);
        $data=["newowner"=>$newowner];
        $ret=$this->sentPut($url, $data);
        
        return isset($ret['data']['newowner'])?$ret['data']['newowner']:false;
      
    }
    
    function addUser($groupid,$username){
        
        $url=$this->geturl('chatgroups/'.$groupid.'/users/'.$username);
        $ret=$this->sentPost($url, []);
        return isset($ret['data']['result'])?$ret['data']['result']:false;
       
    }
    
    function delUser($groupid,$username){
       
        $url=$this->geturl('chatgroups/'.$groupid.'/users/'.$username);
        $ret=$this->sentDelete($url);
        return isset($ret['data']['result'])?$ret['data']['result']:false;
        
    }
}